const CONTROLLER = require('./controller')
const topics = require('./model/TOPIC')

module.exports = async (ctx) => {
    const { io, socket, reg, db, user } = ctx
    Object.assign(ctx, topics)
    Object.keys(CONTROLLER).forEach(async x => {
        const fn = CONTROLLER[x]
        switch (fn.type) {
            case 'broadcast':
                socket.on(x, async data => {
                    io.emit(x, await fn(data, ctx, x))
                })
                break
            case 'peer':
                socket.on(x, async data => {
                    socket.emit(x, await fn(data, ctx, x))
                })
                break
            case 'exec':
                socket.on(x, async (data, cb) => {
                    cb(await fn(data, ctx, x))
                })
                break
            default:
                socket.on(x, async data => await fn(data, ctx, x))
        }
    })
}